﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZSPG_ZZBCJEInfoControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            if (!base.Page.IsPostBack) {
                Bind();
            }
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            CustomGridList1.OnLoadData += new EventHandler(CustomGridList1_OnLoadData);
            Button btnsearch = (Button)FindControl("btnSeach");
            btnsearch.Click += new EventHandler(Go_Search);
        }
        protected void Go_Search(object sender, EventArgs e)
        {
            Bind();
        }
        public void Bind()
        {
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");

            Label lbXmNum = (Label)FindControl("lbXmNum");
            Label lbdzje = (Label)FindControl("lbdzje");
            Label lbhzje = (Label)FindControl("lbhzje");
            Label lbye = (Label)FindControl("lbye");
            Label Label1 = (Label)FindControl("Label1");

            Label Label2 = (Label)FindControl("Label2");
            Label Label3 = (Label)FindControl("Label3");
            Label Label4 = (Label)FindControl("Label4");
            Label Label5 = (Label)FindControl("Label5");
            Label Label6 = (Label)FindControl("Label6");

            DataTable dt = _EluZSDataProvider.GetZZBCJHZ(getWhere());
            string xmNum = GetCompensateBasicNum();
            if (dt.Rows.Count > 0)
            {
                List<string> lis = new List<string>();
                lis.Add("分户建筑面积");
                lis.Add("地大于房土地面积");
                lis.Add("评估单价");
                lis.Add("评估总价");
                lis.Add("补偿金额合计");
                lis.Add("产权人补偿款");
                lis.Add("承租人补偿款");
                _EluZSDataProvider.AddCollectRow(dt, "编号", lis);
            }
            if (dt.Rows.Count > 0)
            {
                lbXmNum.Text = "住宅分户评估总数为：" + xmNum + "个  ";
                lbdzje.Text = "分户建筑面积：" + string.Format("{0:N}", dt.Rows[dt.Rows.Count - 1]["分户建筑面积"]) + " m2  ";
                lbhzje.Text = "地大于房土地面积：" + string.Format("{0:N}", dt.Rows[dt.Rows.Count - 1]["地大于房土地面积"]) + " m2  ";
                lbye.Text = "评估单价：" + string.Format("{0:N}", dt.Rows[dt.Rows.Count - 1]["评估单价"]) + " 元  ";
                Label3.Text = "评估总价：" + string.Format("{0:N}", dt.Rows[dt.Rows.Count - 1]["评估总价"]) + " 元  ";
                Label4.Text = "补偿金额合计：" + string.Format("{0:N}", dt.Rows[dt.Rows.Count - 1]["补偿金额合计"]) + " 元  ";
                Label5.Text = "产权人补偿款：" + string.Format("{0:N}", dt.Rows[dt.Rows.Count - 1]["产权人补偿款"]) + " 元  ";
                Label6.Text = "承租人补偿款：" + string.Format("{0:N}", dt.Rows[dt.Rows.Count - 1]["承租人补偿款"]) + " 元  ";
                string tt1 = dt.Rows[dt.Rows.Count - 1]["评估总价"].ToString();
                string tt2 = dt.Rows[dt.Rows.Count - 1]["补偿金额合计"].ToString();
                Double t1 = 0; Double t2 = 0; int tcount = 0;
                if (!string.IsNullOrEmpty(xmNum)) { tcount = Convert.ToInt32(xmNum); }
                if (!string.IsNullOrEmpty(tt1)) { t1 = Convert.ToDouble(tt1); }
                if (!string.IsNullOrEmpty(tt2)) { t2 = Convert.ToDouble(tt2); }
                Double temp1 = Convert.ToDouble(t1 / tcount);
                Double temp2 = Convert.ToDouble(t2 / tcount);
                Label1.Text = "评估总价平均：" + string.Format("{0:N}", temp1) + " 元 ";
                Label2.Text = "合计补偿金额平均：" + string.Format("{0:N}", temp2) + " 元 ";
                dt.Rows.RemoveAt(dt.Rows.Count - 1);
            }
            else
            {
                lbXmNum.Text = "住宅分户评估总数为：0个  ";
                lbdzje.Text = "分户建筑面积：0 m2  ";
                lbhzje.Text = "地大于房土地面积：0 m2  ";
                lbye.Text = "评估单价：0 元  ";
                Label3.Text = "评估总价：0 元  ";
                Label4.Text = "补偿金额合计：0 元  ";
                Label5.Text = "承租人补偿款：0 元  ";
                Label6.Text = "征收评估总金额：0 元  ";
                Label1.Text = "评估总价平均：0 元 ";
                Label2.Text = "合计补偿金额平均：0 元 ";
            }
            CustomGridList1.DataSource = dt;
            CustomGridList1.RecordCount = dt.Rows.Count;
            CustomGridList1.DataBind();
        }
        protected void CustomGridList1_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }
        private string getWhere()
        {
            TextBox txtCQR = (TextBox)FindControl("txtCQR");
            TextBox txtCZR = (TextBox)FindControl("txtCZR");
            TextBox txtFWZL = (TextBox)FindControl("txtFWZL");
            StringBuilder sbWhere = new StringBuilder();
            string strCQR = txtCQR.Text;
            string strCZR = txtCZR.Text;
            string strFWZL = txtFWZL.Text;
            string PID = base.Page.Request["PID"];
            if (!string.IsNullOrEmpty(strCQR)) { sbWhere.AppendFormat(" and z.产权人 like '%{0}%'", strCQR); }
            if (!string.IsNullOrEmpty(strCZR)) { sbWhere.AppendFormat(" and z.承租人 like '%{0}%'", strCZR); }
            if (!string.IsNullOrEmpty(strFWZL)) { sbWhere.AppendFormat(" and z.房屋坐落 like '%{0}%'", strFWZL); }
            if (!string.IsNullOrEmpty(PID)) { sbWhere.AppendFormat(" and t.pid= '{0}'", PID); }
            return sbWhere.ToString();
        }
        public string GetCompensateBasicNum()
        {
            DataTable dt = _EluZSDataProvider.GetZZBCJHZ(getWhere());
            return dt.Rows.Count.ToString();
        }
    }
}


